﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
public partial class Manage_DecStatus : System.Web.UI.Page, IAccess
{
    string format = "当前已解密企业 {0} 个，已解密产品 {1} 个，未解密企业 {2} 个，未解密产品 {3}个";
    protected void Page_Load(object sender, EventArgs e)
    {
        CurrentAccount.CheckSessionTimeOut(this);
        if (!IsPostBack)
        {
            var pc = BLL_Factory.IBLL_Manage.GetProjectConfig(CurrentAccount.ProjectId);
            btnExport.Enabled = (bool)pc.DecSwitch ? false : true;//开关关闭后才让导出
            BindData();
        }
    }

    private void BindData()
    {
        GetCount();
        int totalrecord;
        //var datasource =IList<HZ.BidSystem.Model.STAT_BidStatus>;
        if (!string.IsNullOrEmpty(GetAppend()))
        {
            var datasource = BLL_Factory.IBLL_Manage.GetDecStatus(new { CompanyName = txtCompanyName.Text.Trim(), Append = GetAppend(), SortFields = "UserCode" }, pager1.CurrentPage, pager1.PageSize, out totalrecord);
            pager1.RecordCount = totalrecord;
            gv1.DataSource = datasource;
            gv1.DataBind();
        }
        else
        {
            var datasource = BLL_Factory.IBLL_Manage.GetDecStatus(new { CompanyName = txtCompanyName.Text.Trim(), SortFields = "UserCode" }, pager1.CurrentPage, pager1.PageSize, out totalrecord);
            pager1.RecordCount = totalrecord;
            gv1.DataSource = datasource;
            gv1.DataBind();
        }

    }
    private string GetAppend()
    {
        string s = "";
        if (ddl.SelectedValue != "全部")
        {
            switch (ddl.SelectedValue)
            {
                case "0":
                    s = "DecStatus = 0 AND ConfirmStatus=1";
                    break;
                case "1":
                    s = "DecStatus =1 AND ConfirmStatus=1";
                    break;
                default:
                    s = "";
                    break;
            }
        }
        return s;
    }
    protected void btnQeury_Click(object sender, EventArgs e)
    {
        pager1.CurrentPage = 1;
        BindData();

    }
    protected void pager1_PageChanging(object sender, HZ.Web.PageChaningEventArgs e)
    {
        BindData();

    }

    private void GetCount()
    {
        DataTable dtcount = BLL_Factory.IBLL_Common.GetDecCount();
        if (dtcount.Rows.Count > 0)
        {
            int allcompanycount = Convert.ToInt32(dtcount.Rows[0]["企业总数"]);
            int allgoodscount = Convert.ToInt32(dtcount.Rows[0]["产品总数"]);
            int bidgoodscount = 0;
            if (!string.IsNullOrEmpty(dtcount.Rows[0]["产品解密数"].ToString()))
            {
                bidgoodscount = Convert.ToInt32(dtcount.Rows[0]["产品解密数"]);
            }
            int bidcompanycount = 0;
            if (!string.IsNullOrEmpty(dtcount.Rows[0]["已解密企业数"].ToString()))
            {
                bidcompanycount = Convert.ToInt32(dtcount.Rows[0]["已解密企业数"]);
            }


            /* 写法太复杂，拖累系统 by yjx 2016年8月15日 15:28:00
            //所有已解密的企业数
            bidcompanycount = (int)HZ.Data.DbHelperFactory.Create().ExecuteScalar("SELECT COUNT(DISTINCT CompanyId) FROM dbo.TB_Project_Bid WHERE DecPrice IS NOT NULL");
            //数据类型准换,转换前判断是否为空 updated by zhouya 20160620
            //已确认，且已报价的产品数 by yjx 2016年8月15日 14:56:25
            object totalCountobj = HZ.Data.DbHelperFactory.Create().ExecuteScalar("SELECT COUNT(BidId) FROM dbo.TB_Project_Bid WHERE EncryptPrice IS NOT NULL and CompanyId in (select CompanyId from TB_Company_Project_Switch where ConfirmStatus = 1)");
            //已解密的产品数 by yjx 2016年8月15日 14:59:30
            object bidgoodscount1obj = HZ.Data.DbHelperFactory.Create().ExecuteScalar("SELECT COUNT(BidId) FROM dbo.TB_Project_Bid WHERE DecPrice is not null");
            //已确认，且已报价的企业数 by yjx 2016年8月15日 14:56:25
            object totalcompanyobj = HZ.Data.DbHelperFactory.Create().ExecuteScalar("SELECT COUNT(DISTINCT CompanyId) FROM dbo.TB_Project_Bid WHERE EncryptPrice IS NOT NULL and CompanyId in (select CompanyId from TB_Company_Project_Switch where ConfirmStatus = 1)");
            //已解密的企业数 by yjx 2016年8月15日 14:59:30
            object bidcompanyobj = HZ.Data.DbHelperFactory.Create().ExecuteScalar("SELECT COUNT(CompanyId) FROM dbo.TB_Company_Project_Switch WHERE DecStatus=1");

            int totalcount = 0;
            int bidgoodscount1 = 0;
            int totalcompany = 0;
            int bidcompany = 0;

            if (totalCountobj != null && totalCountobj != DBNull.Value)
            {
                totalcount = (int)totalCountobj;
            }

            if (bidgoodscount1obj != null && bidgoodscount1obj != DBNull.Value)
            {
                bidgoodscount1 = (int)bidgoodscount1obj;
            }

            if (totalcompanyobj != null && totalcompanyobj != DBNull.Value)
            {
                totalcompany = (int)totalcompanyobj;
            }

            if (bidcompanyobj != null && bidcompanyobj != DBNull.Value)
            {
                bidcompany = (int)bidcompanyobj;
            }

             */
            // string format = "当前已解密企业 {0} 个，已解密品种 {1} 个，未解密企业 {2} 个，未解密品种 {3}个";
            string r = string.Format(format, bidcompanycount, bidgoodscount, allcompanycount - bidcompanycount, allgoodscount - bidgoodscount);
            //为解密的企业与未解密的产品 2016-6-15
            //string r = string.Format(format, bidcompanycount, bidgoodscount, totalcompany - bidcompany, totalcount - bidgoodscount1);
            ltlinfo.Text = r;
        }



    }
    #region IAccess Members

    IList<EnumAccess> IAccess.AccessList
    {
        get
        {
            var list = new List<EnumAccess>();
            list.Add(EnumAccess.Manage);
            return list;
        }

    }

    #endregion
    protected void btnExport_Click(object sender, EventArgs e)
    {
        HZ.Web.WebUtil.ExportToExcel(HZ.Data.DbHelperFactory.Create().ExecuteTable(@"select  
  Company_Goods 流水号, ProductName 通用名, model 剂型, Outlookc 规格, PackFactor 转换系数,MaterialUnit 最小制剂单位,PackUnit 包装单位, QualityLevelName 竞价组, CompanyName_SC 生产企业, CompanyName_tb 投标企业, PackMaterials 包装,Weights [装/重量], decprice 企业报价,case when ConfirmStatus=1  and EncryptPrice is not null then '已报价且确认' when ConfirmStatus=0 and EncryptPrice is not null then '已报价未确认' else '未报价' end 报价状态,case when  DecStatus=1 then '解密成功' when ConfirmStatus=0 then '未确认报价无需解密' else '未解密' end 解密状态, DecTime 解密时间 
 from View_DecStatus"), "DecpriceALL.xls");
    }

    protected void btnNotBid_Click(object sender, EventArgs e)
    {
        var dt = HZ.Data.DbHelperFactory.Create().ExecuteTable("  SELECT DISTINCT ProductId 竞价组号,ProductCode 招标编码,BidMuLuCode 招标通用名,DirectoryCode 招标剂型,BidTableCode 招标规格  FROM View_BidPrice WHERE ProductId not IN (SELECT ProductId FROM dbo.TB_GoodsInfo WHERE Companyid_SC IN (SELECT CompanyId FROM dbo.TB_Company_Project_Switch WHERE ConfirmStatus = 1 AND DecStatus =1 )) AND ProductId  IN (	SELECT ProductId FROM dbo.TB_GoodsInfo WHERE Companyid_SC IN (SELECT CompanyId FROM dbo.TB_Company_Project_Switch WHERE ConfirmStatus = 1)) ");
        HZ.Web.WebUtil.ExportToExcel(dt, "NotBidDatelist.xls");
    }


    protected void btnNotChe_Click(object sender, EventArgs e)
    {
        var dt = HZ.Data.DbHelperFactory.Create().ExecuteTable("  SELECT DISTINCT ProductId 竞价组号,ProductCode 招标编码,BidMuLuCode 招标通用名,DirectoryCode 招标剂型,BidTableCode 招标规格  FROM View_BidPrice WHERE ProductId not IN (SELECT ProductId FROM dbo.TB_GoodsInfo WHERE Companyid_SC IN (SELECT CompanyId FROM dbo.TB_Company_Project_Switch WHERE ConfirmStatus = 1 AND DecStatus =1 AND DecPrice = 0  )) AND ProductId IN (	SELECT ProductId FROM dbo.TB_GoodsInfo WHERE Companyid_SC IN (SELECT CompanyId FROM dbo.TB_Company_Project_Switch WHERE ConfirmStatus = 1)) ");
        HZ.Web.WebUtil.ExportToExcel(dt, "NotCheDatelist.xls");
    }
}
